/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slsBeans;

import EL.*;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Tian
 */
@Stateless
public class FoodTypeBLO implements FoodTypeBLORemote
{

    @PersistenceContext(unitName = "Tunga_EJB3PU")
    private EntityManager em;

    public List<FoodTypes> getAll()
    {
        String hql = "From FoodTypes";
        Query query = this.em.createQuery(hql);
        List<FoodTypes> foodTypes = query.getResultList();
        return foodTypes;
    }

    public FoodTypes getByID(int id)
    {
        FoodTypes foodType = this.em.find(FoodTypes.class, id);
        return foodType;
    }

    public boolean add(FoodTypes foodType)
    {
        try
        {
            this.em.persist(foodType);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean edit(FoodTypes foodType)
    {
        try
        {

            this.em.merge(foodType);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean delete(int id)
    {
        FoodTypes foodType = getByID(id);
        if (foodType != null)
        {
            try
            {
                this.em.remove(foodType);
                return true;
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
                return false;
            }
        }
        return false;
    }
}
